package com.jyb.util.capture;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.jyb.config.HttpCilentConfig;
import com.jyb.util.UrlUtil;
import com.jyb.vo.YpcdVo;

public class YpcdUtil {

	public static List<YpcdVo> capture(String tdh){
		List<YpcdVo> list=new ArrayList<YpcdVo>();
		
		CloseableHttpClient httpclient = HttpClients.createDefault();
		CloseableHttpResponse response=null;
		try {
			
			String url="http://edi.easipass.com/dataportal/query.do?qn=dp_premanifest_bill_list&blno="+tdh+"&vslname=&voyage=";
			url=UrlUtil.hanadleUrl(url);
			HttpGet httpGet = HttpCilentConfig.getHttpGet(url);
			
			response = httpclient.execute(httpGet);
			HttpEntity entity = response.getEntity();
			String str=EntityUtils.toString(entity);
			Document document=Jsoup.parse(str);
			
			
			Elements elements=document.select("table").get(1).select("tr:gt(0)");
			for (Element element : elements) {
				Elements elementTds=element.select("td");
				YpcdVo ypcdVo=new YpcdVo();
				ypcdVo.setLx("0");
				ypcdVo.setCm(elementTds.get(0).text());
				ypcdVo.setCc(elementTds.get(1).text());
				ypcdVo.setTdh(elementTds.get(2).text());
				
				try {
					String url2="http://edi.easipass.com/dataportal/query.do?qn=dp_premanifest_bill_detail&blno="+ypcdVo.getTdh()+"&vslname="+ypcdVo.getCm()+"&voyage="+ypcdVo.getCc();
					url2=UrlUtil.hanadleUrl(url2);
					httpGet = HttpCilentConfig.getHttpGet(url2);
					response = httpclient.execute(httpGet);
					entity = response.getEntity();
					str=EntityUtils.toString(entity);
					document=Jsoup.parse(str);
					Elements trs=document.select("table").get(2).select("tr");
					ypcdVo.setYpcdzt(trs.get(0).select("td").get(1).text());
					ypcdVo.setZxsdsj(trs.get(1).select("td").get(1).text());
					ypcdVo.setYpcdbwh(trs.get(2).select("td").get(1).text());
				} catch (Exception e) {
					// TODO: handle exception
				}
				
				list.add(ypcdVo);
			}
			
			url="http://edi.easipass.com/dataportal/query.do?qn=dp_premanifest_ack_list&blno="+tdh+"&vslname=&voyage=";
			url=UrlUtil.hanadleUrl(url);
			httpGet = HttpCilentConfig.getHttpGet(url);
			
			response = httpclient.execute(httpGet);
			entity = response.getEntity();
			str=EntityUtils.toString(entity);
			document=Jsoup.parse(str);
			
			elements=document.select("table").get(1).select("tr:gt(0)");
			for (Element element : elements) {
				Elements elementTds=element.select("td");
				YpcdVo ypcdVo=new YpcdVo();
				ypcdVo.setLx("1");
				ypcdVo.setCm(elementTds.get(0).text());
				ypcdVo.setCc(elementTds.get(1).text());
				ypcdVo.setTdh(elementTds.get(2).text());
				
				try {
					String url2="http://edi.easipass.com/dataportal/query.do?qn=dp_premanifest_bill_detail&blno="+ypcdVo.getTdh()+"&vslname="+ypcdVo.getCm()+"&voyage="+ypcdVo.getCc();
					url2=UrlUtil.hanadleUrl(url2);
					httpGet = HttpCilentConfig.getHttpGet(url2);
					response = httpclient.execute(httpGet);
					entity = response.getEntity();
					str=EntityUtils.toString(entity);
					document=Jsoup.parse(str);
					Elements trs=document.select("table").get(2).select("tr");
					ypcdVo.setYpcdzt(trs.get(0).select("td").get(1).text());
					ypcdVo.setZxsdsj(trs.get(1).select("td").get(1).text());
					ypcdVo.setYpcdbwh(trs.get(2).select("td").get(1).text());
				} catch (Exception e) {
					// TODO: handle exception
				}
				
				list.add(ypcdVo);
			}
			
			//System.out.println(str);
			
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(response!=null){
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		return list;
	}
	
	public static void main(String[] args) {
		YpcdUtil.capture("SITGSHSIA30018");
	}
}
